package com.educate.servicebase.exceptionhander;

import com.educate.commonutils.ResultOne;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody//为了返回数据
    public ResultOne error(Exception e){
        e.printStackTrace();
        return ResultOne.error().message("执行了全局异常处理");
    }
    /**
     * 特定异常
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody//为了返回数据
    public ResultOne error(ArithmeticException e){
        e.printStackTrace();
        return ResultOne.error().message("执行了ArithmeticException异常处理");
    }
    /**
     * 自定义异常类
     */
    @ExceptionHandler(EducateException.class)
    @ResponseBody//为了返回数据
    public ResultOne error(EducateException e){
        log.error(e.getMessage());
        e.printStackTrace();
        return ResultOne.error().code(e.getCode()).message(e.getMsg());
    }
}
